/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : UserDataHelper.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * May 13, 2011  mycodetalks			       Created
 */
package net.lwf.web.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.lwf.constant.IDalConstant;
import net.lwf.entity.TabDisplayInfo;
import net.lwf.entity.WebUser;
import net.lwf.hibernate.entity.User;
import net.lwf.hibernate.entity.lookup.Lookup;
import net.lwf.jaxb.service.TabMenuServiceFactory;
import net.lwf.jaxb.stubs.tabmenu.SubjectInfo;
import net.lwf.service.LookupService;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IWebConstant;

/**
 * Class description goes here
 */
public class UserDataHelper {
	
	public static List<Lookup> getUserGrades() {
		List<Lookup> grades = new ArrayList<Lookup>();
		Lookup lkp = new Lookup();
		lkp.setKey(" ");
		lkp.setValue(" -- Select grade --");
		grades.add(lkp);
		//grades.addAll(LookupFactory.getInstance().getData(IDalConstant.GRADES));
		grades.addAll(new LookupService().getData(IDalConstant.GRADES));
		return grades;
	}
	
	//check references and change to private if needed
	public static void setBaseUserInfo(HttpServletRequest request, WebUser user) {

		WebUtil.setAttribute(request, IWebConstant.WEB_USER, user);
		WebUtil.setAttribute(request, IWebConstant.TAB_MENU_INFO,
				getMenuInfo(user.getUser().getGrade()));
	}
	
	public static WebUser getWebUser(HttpServletRequest request) {
		WebUser user = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		if(user != null) {
			return user;
		}
		return null;
	}
	
	public static void checkGrSubTop(HttpServletRequest request) {
		isGradeChanged(request);
		isSubjectChanged(request);
		isTopicChanged(request);
	}
	
	/**
	 * Allowing this process for the time being so that user can switch the grades.
	 * Later the switching will be controlled via user roles.
	 * 
	 * @param request
	 */
	private static void isGradeChanged(HttpServletRequest request) {
		String chosengrade = request.getParameter("chosengrade");
		if(chosengrade != null) {
			WebUser webuser = getWebUser(request);
			User user = webuser.getUser();
			user.setGrade(chosengrade);
			UserDataHelper.setBaseUserInfo(request, webuser);
			WebUtil.removeAttribute(request, IWebConstant.SUBJECT);
			WebUtil.removeAttribute(request, IWebConstant.TOPIC);
		}
	}
	
	private static void isSubjectChanged(HttpServletRequest request) {
		String subInReq	 = request.getParameter(IWebConstant.SUBJECT);
		if(subInReq != null) {
			WebUtil.removeAttribute(request, IWebConstant.TOPIC);
			WebUtil.setAttribute(request, IWebConstant.SUBJECT, subInReq);
		}
	}
	
	private static void isTopicChanged(HttpServletRequest request) {
		String topicInReq = request.getParameter(IWebConstant.TOPIC);
		if(topicInReq != null) {
			WebUtil.setAttribute(request, IWebConstant.TOPIC, topicInReq);
		}
	}
	
	private static List<TabDisplayInfo> getMenuInfo(String grade) {
		List<TabDisplayInfo> newMenu = new ArrayList<TabDisplayInfo>();
		List<SubjectInfo> menuInfo = TabMenuServiceFactory.getInstance()
				.getMenu(grade);
		int menuSize = menuInfo.size();
		int padUpMenuTabs = 9 - menuSize;
		TabDisplayInfo display = null;
		for (SubjectInfo info : menuInfo) {
			display = new TabDisplayInfo();
			display.setName(info.getName());
			display.setTopicInfo(info.getTopicInfo());
			newMenu.add(display);
		}
		for (int i = 0; i < padUpMenuTabs; i++) {
			display = new TabDisplayInfo();
			display.setEmptyTab(true);
//			if (i + 1 == padUpMenuTabs) {
//				display.setTdWidth("4%");
//			}
			newMenu.add(display);
		}
		return newMenu;
	}

}



